package com.baichuan.zntx.domain;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baichuan.common.core.utils.DateUtils;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class DataMessage extends BaseMessage{
    private String id;
    private String battery;
    private Position position;
    private Location location;
    private boolean islLocationValid;
    private boolean hasTrack;
    private boolean hasWheel;
    private boolean hasRfid;
    private String rfidCode;
    private Integer temperature;
    private Date time;
    private String ipAddress;
    private String dataContent;
    public DataMessage(String ip, String content){
        ipAddress = ip;
        if(ObjectUtil.isNotEmpty(content)){
            dataContent = content;
            String[] data = content.split(",");
            if(data.length == 15){
                if(data[0].contains("$")){
                    this.setMsgType(data[0].split("\\$")[1]);
                }else{
                    this.setMsgType(data[0]);
                }
                this.setId(data[1]);
                this.setBattery(data[2]);
                this.setPosition(new Position(Double.parseDouble(data[3]), Double.parseDouble(data[4]), Double.parseDouble(data[5])));
                this.setLocation(new Location(Double.parseDouble(data[6]), Double.parseDouble(data[7])));
                this.setIslLocationValid("1".equals(data[8]));
                this.setHasTrack("0".equals(data[10]));
                this.setHasWheel("0".equals(data[9]));
                if(Integer.parseInt(data[11]) > 0){
                    this.setRfidCode(data[11]);
                    this.setHasRfid(true);
                }else{
                    this.setHasRfid(false);
                }
                this.setTemperature(Integer.parseInt(data[12]));
                this.setTime(DateUtils.getNowDate());
            }
        }
    }

    @Override
    public String toString(){
        return "$" + getMsgTypeStr() +
                "," + getId() +
                "," + getBattery() +
                "," + getPosition().toString() +
                "," + getLocation().toString() +
                "," + (islLocationValid ? 1 : 0) +
                "," + (hasTrack ? 1 : 0) +
                "," + (hasWheel ? 1 : 0) +
                "," + (hasRfid ? 1 : 0) +
                "," + getTemperature() +
                "," + DateUtils.getNowDate().toString() +
                "," + "end/n";
    }

}
